package com.foreknow.elm.controller;

import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryAddressController {
    DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
//DeliveryAddressController/listDeliveryAddressByUserId
    public List<DeliveryAddress> listDeliveryAddressByUserId(HttpServletRequest request)throws Exception{
        String userid = request.getParameter("userId");
        List<DeliveryAddress> list = deliveryAddressService.squeryById(userid);
        return list;
    }
//DeliveryAddressController/getDeliveryAddressById
    public DeliveryAddress getDeliveryAddressById(HttpServletRequest request)throws Exception{
        String daId = request.getParameter("daId");
        int daid = Integer.parseInt(daId);
        DeliveryAddress deliveryAddress = deliveryAddressService.squeryDABbId(daid);
        return deliveryAddress;
    }
//DeliveryAddressController/saveDeliveryAddress
    public int saveDeliveryAddress(HttpServletRequest request)throws Exception{
        String name = request.getParameter("contactName");
        String s = request.getParameter("contactSex");
        int sex = Integer.parseInt(s);
        String tel =  request.getParameter("contactTel");
        String add = request.getParameter("address");
        String uid = request.getParameter("userId");
        int a = deliveryAddressService.sinsert(name,sex,tel,add,uid);
        return a;
    }
//DeliveryAddressController/updateDeliveryAddress
    public int updateDeliveryAddress(HttpServletRequest request)throws Exception{
        String d = request.getParameter("daId");
        int did = Integer.parseInt(d);
        String name = request.getParameter("contactName");
        String s = request.getParameter("contactSex");
        int sex = Integer.parseInt(s);
        String tel =  request.getParameter("contactTel");
        String add = request.getParameter("address");
        String uid = request.getParameter("userId");
        DeliveryAddress address = new DeliveryAddress();
        address.setDaId(did);
        address.setContactName(name);
        address.setContactSex(sex);
        address.setContactTel(tel);
        address.setAddress(add);
        address.setUserId(uid);
        int a = deliveryAddressService.supdate(address);
        return a;
    }
//DeliveryAddressController/removeDeliveryAddress
    public int removeDeliveryAddress(HttpServletRequest request)throws Exception{
        String i = request.getParameter("daId");
        int daid =  Integer.parseInt(i);
        int a = deliveryAddressService.sdelete(daid);
        return a;
    }
}
